#include "TcpServer.hpp"
#include "log.hpp"
#include "Calculator.hpp"
#include<memory>

Calculator calculator;


void DefaultOnMessage(std::weak_ptr<Connection> conn)
{
    if(conn.expired()) return;
    auto connection = conn.lock();

    //对报文进行处理
    std::cout<<"上层得到了数据： "<<connection->InBuffer() <<std::endl;
    std::string response_str = calculator.Handler(connection->InBuffer());  //业务逻辑比较简单，没有特别耗时的工作
    if(response_str.empty()) return;
    logMessage(DEBUG, "%s", response_str.c_str());

    //发送出去
    connection->AppendOutBuffer(response_str);

    auto tcpserver = connection->_tcp_server_ptr.lock();
    tcpserver->Sender(connection);
}


int main()
{

    std::shared_ptr<TcpServer> svr(new TcpServer(8888, DefaultOnMessage));
    svr->Init();
    svr->Loop();
    
    return 0;
}